"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : MOT
Case Name   : MOT表查询时索引条件中含有相同列
Create At   : 2023/02/16
Owner       : @kyeleze
Description :
    1、修设置enable_incremental_checkpoint参数为off,并重启数据库
    2、创建MOT表，并插入数据
    3、执行查询sql，索引条件中含有相同列
Expect      :
    1、成功
    2、成功
    3、sql执行成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class MotDatatypeTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.constant = Constant()
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.common = Common()
        self.schema = 'schema_mot_0298'
        self.table_name = 't_mot_table_0298'
        self.incremental_checkpoint = \
            self.common.show_param("enable_incremental_checkpoint")

    def test_mot_datatype(self):
        text = "--step1:修设置enable_incremental_checkpoint参数为off,并重启数据库" \
               "expect:成功--"
        self.log.info(text)
        status = self.primary_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            'enable_incremental_checkpoint=off')
        self.assertTrue(status, "执行失败:" + text)
        restart_msg = self.primary_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status_msg = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" or "Degrade" in status_msg)
        show_msg = self.common.show_param("enable_incremental_checkpoint")
        self.assertIn('off', show_msg, '执行失败' + text)

        text = "--step2:创建MOT表，并插入数据 expect:成功--"
        self.log.info(text)
        self.log.info('--创建MOT表--')
        sql_cmd = f'drop schema if exists {self.schema};' \
                  f'drop table if exists {self.table_name};' \
                  f'create schema {self.schema};' \
                  f'create table {self.schema}.{self.table_name}' \
                  f'(col1 int primary key, col2 int);' \
                  f'insert into {self.schema}.{self.table_name} ' \
                  f'values(generate_series(1,10), 1);'
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG,
                      result, '执行失败' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS,
                      result, '执行失败' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      result, '执行失败' + text)

        text = "--step3:执行查询sql，索引条件中含有相同列 expect:成功--"
        self.log.info(text)
        sql_cmd = f'select * from {self.schema}.{self.table_name} ' \
                  f'where col1 = col1 * 1;'
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn('10 rows', result, '执行失败' + text)

    def tearDown(self):
        text = '--清理环境--'
        self.log.info(text)
        result = self.primary_sh.execut_db_sql(f'drop schema {self.schema} '
                                               f'cascade;')
        self.log.info(result)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG,
                      result, '执行失败' + text)
        status = self.primary_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_incremental_checkpoint={self.incremental_checkpoint}')
        self.assertTrue(status, "执行失败:" + text)
        restart_msg = self.primary_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status_msg = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" or "Degrade" in status_msg)
        show_msg = self.common.show_param("enable_incremental_checkpoint")
        self.assertIn(self.incremental_checkpoint, show_msg, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end----')

